<?php

class registration extends Model
{

	function isRegistered($studno, $eventid) {
		$rs = $this->db->getAll(
			"SELECT registration_id
			FROM registration
			WHERE stud_no = ?
			AND event_id = ?",
			array( $studno, (int) $eventid)
		);
		if( count($rs) > 0 ) return true;
		else return false;
	}
	

	function getReg($id)
	{
		$greg = $this->db->getAll("SELECT 
				organization.name,
				event.name,
				user.lastname,
				user.firstname,
				user.middlename
			FROM event, user, organization, registration
			WHERE registration.org_id = organization.org_id
			AND registration.stud_no = user.stud_no
			AND event.event_id = registration.event_id
			AND registration.registration_id = ?",
			array( (int) $id )	
		);
		return $greg;
	}
	
	function getRegList( $eventid )
	{
		$greg = $this->db->getAll("SELECT 
				u.lastname,
				u.firstname
				FROM registration r
				INNER JOIN user u
				ON r.stud_no = u.stud_no
				WHERE r.event_id = ?
				ORDER BY lastname ASC
				",
				array( (int) $eventid )
		);
		return $greg;
	}
	
	
	function insertReg( $event_id, $org_id, $stud_no )
	{
		$ireg = $this->db->execute("INSERT INTO registration( event_id,
					org_id,
					stud_no )
			VALUES ( ?, ?, ? )", 
			array( (int) $event_id, (int) $org_id, $stud_no )
		);
		return $ireg;
	}

	function updateReg( $event_id, $org_id, $stud_no, $registation_id ) 
	{
		$ureg = $this->db->execute("UPDATE registration
			SET event_id = ?,
			org_id = ?,
			stud_no = ?
			WHERE registration_id = ?",
			array( (int) $event_id, (int) $org_id, $stud_no, (int) $registation_id )
		);
		return $ureg;
	}

	function deleteReg( $registration_id )
	{
		$dreg = $this->db->execute("DELETE FROM registration
			WHERE registration_id = ? ",
			array( (int) $registration_id )
		);
	}
}